package paquete_aux;

import java.io.UnsupportedEncodingException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

public class GeneradorDeParejaLlavesRSA {

	public static void main(String[] args) {
		try {
			new GeneradorDeParejaLlavesRSA();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}

	public GeneradorDeParejaLlavesRSA() throws NoSuchAlgorithmException {
		genrearParejas();
		System.out.println("------------------------------------");
		genrearParejas();
	}
	private void genrearParejas() throws NoSuchAlgorithmException {
		KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
		generator.initialize(1024);
		KeyPair keypair = generator.generateKeyPair();
		PrivateKey priv = keypair.getPrivate();
		PublicKey pub = keypair.getPublic();

		byte[] privada = priv.getEncoded();
		byte[] publica = pub.getEncoded();
		
		String privadaPaMandar=transformar(privada);
		String pubicaPaMandar=transformar(publica);
		System.out.println(privadaPaMandar);
		System.out.println(pubicaPaMandar);
		System.out.println();
	}

	/**
	 * @param b
	 * @return
	 */
	public String transformar(byte[] b) {

		String s = null;

		try {
			s = new String("".getBytes(),Comun.COD);

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();

		}
		for (int i = 0; i < b.length; i++) {

			if (i == b.length - 1)
				s += Byte.toString(b[i]);
			else
				s += Byte.toString(b[i]) + Comun.SEPARADOR;
		}
		return s;
	}
}